﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcCarConsume.Domain;
using MvcCarConsume.Domain.Entites;
using System.ComponentModel;
using MvcCarConsume.WebUI;

namespace MvcCarConsume.WebUI
{
    public class CarCartController : BaseController
    {
        private ICarService carService;
        private ICartService cartService;

        public CarCartController(ICarService carService, ICartService cartService)
        {
            this.carService = carService;
            this.cartService = cartService;
        }

        [Authorize]       
        public ActionResult PostCart(int id)
        {
            ViewData["CarId"] = id;
            CarInView content = null;
            var queryResult = carService.CarWithID(id);
            if (queryResult != null)
            {
                content = new CarInView()
                {
                    CarID = queryResult.CarId,
                    CarName = queryResult.CarName,
                    CarPrice = queryResult.Price,
                    CarDescription = queryResult.Description,
                    CarCount = queryResult.Count
                };
                return View(content);
            }
            else
                return View(content);
        }

        [Authorize]
        public ActionResult AddToOrder(int count, int days, string starttime, int id)
        {
            //if (User.Identity.Name == "")
            //{
            //    return Redirect("/Account/LogIn");
            //}
            if (!cartService.CheckNumOfCars(id, count))
            {
                TempData["Error"] = "The number of cars you inputted must lower than the maximum number";
                return Redirect("/orders/add/" + id.ToString());
            }
            if (!cartService.SaveCartInfo(id, count, days, starttime, User.Identity.Name))
            {
                TempData["Error"] = "Failed! Please try again!";
                return Redirect("/orders/add/" + id.ToString());
            }
            TempData["id"] = count.ToString();
            //  Response.
            return Redirect("/orders/");
        }


        //
        // GET: /CarCart/
        [Authorize]
        public ActionResult Index(/*int count*/[DefaultValue(1)]int page)
        {
            var Orders = cartService.GetOrdersByUsername(User.Identity.Name);
            if (Orders != null)
            {
                VMAdminViewOrder vOrders = new VMAdminViewOrder();
                vOrders.LoadCars(Orders, page);
                //foreach (var item in Orders)
                //{
                //    vOrders.Orders.Add(new AdminViewInOrder
                //    {
                //        OrderId = item.OrderId,
                //        BrandId = item.BrandId,
                //        TypeId = item.TypeId,
                //        Count = item.Count,
                //        BorrowDate = item.BorrowDate,
                //        SendDate = item.SendDate,
                //        UserName = item.UserName,
                //        Price = item.Price,
                //        State = item.State
                //    });
                //}
                return View(vOrders);
            }
            else
            {
                TempData["msg"] = "There is nothing to display.";
                return View();
            }
        }

        [Authorize]
        public ActionResult Edit(int id)
        {
            return Redirect("/orders/");
        }

        [Authorize]
        public ActionResult Pay(int id)
        {
            cartService.Pay4OrderById(id);
            return Redirect("/orders/");
        }

        [Authorize]
        public ActionResult Return(int id)
        {
            cartService.ReturnOrderById(id);
            return Redirect("/orders/");
        }

        [Authorize]
        public ActionResult cancel(int id)
        {
            cartService.cancel(id);
            return Redirect("/orders/");
        }
    }
}
